package tw.idv.liuken.android.sip;

import java.util.Random;

import tw.idv.liuken.android.sip.R;

import android.app.Activity;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
	private SipWrapper mSipWrapper;
	
	private EditText mSipServer, mSipAccount, mSipPassword, mPeer;
	private Button mRegister, mOpen, mMakeCall, mAnswerCall, mEndCall, mMuteAudio, mDtmf, mChangeCall;
	private Random r;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        r = new Random();
        mSipWrapper = new SipWrapper(this);
        
        TextView tv = (TextView)findViewById(R.id.sipinfo);
        if (mSipWrapper.isSipSupport()) {
        	Toast.makeText(this, "Your Android supports SIP.", Toast.LENGTH_LONG).show();
        	tv.setText("Your Android supports SIP.");
        } else {
        	Toast.makeText(this, "Your Android DONOT support SIP!!", Toast.LENGTH_LONG).show();
        	tv.setText("Your Android DONOT support SIP!!");
        }
        
        mSipServer = (EditText)findViewById(R.id.sipserver);
        mSipAccount = (EditText)findViewById(R.id.sipaccount);
        mSipPassword = (EditText)findViewById(R.id.sippassword);
        mPeer = (EditText)findViewById(R.id.peerid);
        mRegister = (Button)findViewById(R.id.register);
        mOpen = (Button)findViewById(R.id.open);
        mMakeCall = (Button)findViewById(R.id.makecall);
        mAnswerCall = (Button)findViewById(R.id.answercall);
        mEndCall = (Button)findViewById(R.id.endcall);
        mMuteAudio = (Button)findViewById(R.id.muteaudio);
        mDtmf = (Button)findViewById(R.id.dtmf);
        mChangeCall = (Button)findViewById(R.id.changecall);
        
        mOpen.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mSipWrapper.setSipProfile(mSipServer.getText().toString(), 
						mSipAccount.getText().toString(), 
						mSipPassword.getText().toString());
				mSipWrapper.open();				
			}
        });
        
        mRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mSipWrapper.register();				
			}
        });
        
        mMakeCall.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.makeCall(mPeer.getText().toString());
        	}
        });
        
        mAnswerCall.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.answerCall();
        	}
        });
        
        mEndCall.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.endCall();
        	}
        });
        
        mMuteAudio.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.toggleMuteAudio();
        	}
        });
        
        mDtmf.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.sendDtmf(r.nextInt(15));
        	}
        });
        
        mChangeCall.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		mSipWrapper.changeCall();
        	}
        });
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSipWrapper != null) {
        	mSipWrapper.close();
        }
    }
}